package com.woniuxy.woniuhis.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.ExaminationName;
import com.woniuxy.woniuhis.service.ExaminationNameService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Controller
@RequestMapping("/examinationname")
public class ExaminationNameController {
	
	@Autowired
	ExaminationNameService examinationNameService;
	
	/**
	 * 插入检查项目
	 */
	@RequestMapping("/add")
	@ResponseBody
	public ResponseJson addExaminationName(@RequestBody ExaminationName exName) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(exName==null ||exName.getExamNameContent() == null ||"".equals(exName.getExamNameContent())) {
				throw new RuntimeException("检查项目名不能为空");
			}
			exName.setExamNameId(UUIDUtil.uuid32());
			examinationNameService.addExaminationName(exName);
			responseJson.setMsg("添加成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}

	/**
	 * 更新检查项目
	 */
	@RequestMapping("/update")
	@ResponseBody
	public ResponseJson modifyExaminationName(@RequestBody ExaminationName exName) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(exName==null ||exName.getExamNameContent() == null ||"".equals(exName.getExamNameContent())) {
				throw new RuntimeException("检查项目名不能为空");
			}
			examinationNameService.modifyExaminationName(exName);
			responseJson.setMsg("更新成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}

	/**
	 * 根据id查询指定检查项目
	 */
	@RequestMapping("/item")
	@ResponseBody
	public ResponseJson findExaminationNameById(String examNameId) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(examNameId==null || "".equals(examNameId)) {
				throw new RuntimeException("不存在该检查项目");
			}
			ExaminationName allExaminationName = examinationNameService.findExaminationNameById(examNameId);
			List<ExaminationName> temp = new ArrayList<ExaminationName>();
			temp.add(allExaminationName);
			responseJson.setData(temp);
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}

	/**
	 * 查询所有检查项目，满足条件的
	 */
	@RequestMapping("/all")
	@ResponseBody
	public ResponseJson<ExaminationName> findAllExaminationName(@RequestBody ExaminationName exName) {
		ResponseJson<ExaminationName> responseJson = new ResponseJson<ExaminationName>();
		try {
			if(exName==null ||exName.getExamNameContent() == null ||"".equals(exName.getExamNameContent())) {
				exName = new ExaminationName();
			}
			Integer page = exName.getPage();
			Integer limit = exName.getLimit();
			if(page == null) {
				page = 1;
			}
			if(limit == null) {
				limit = 10;
			}
			PageInfo<ExaminationName> allExaminationNames = examinationNameService.findAllExaminationName(exName,page,limit);
			responseJson.setData(allExaminationNames.getList());
			responseJson.setCount((int)allExaminationNames.getTotal());
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}

	/**
	 * 删除执行检查项目
	 */
	@RequestMapping("/del")
	@ResponseBody
	public ResponseJson delExaminationNameById(String examNameId) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(examNameId==null || "".equals(examNameId)) {
				throw new RuntimeException("不存在该检查项目");
			}
			examinationNameService.delExaminationNameById(examNameId);
			responseJson.setMsg("删除成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}

}
